﻿using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Odyssey.Controls;
using Odyssey.Controls.Classes;
using System.Windows.Controls.Primitives;
using WpfELearningAuthoringTool;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using ScormModel;
using ScormModel.View;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ScormModel.HtmlBuilder;
using System.Collections.ObjectModel;
using ScormModel.Question;
using System.Collections.Generic;

namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for ExamCompetencyDialog.xaml
    /// </summary>
    public partial class ExamCompetencyDialog : Window
    {
        RibbonBarRegion.RibbonBarHeader parentChild;
        ObservableCollection<QuestionTest> listQuestions = new ObservableCollection<QuestionTest>();
        public ExamCompetencyDialog()
        {
            InitializeComponent();
            listPossibleAnwser.ItemsSource = listQuestions;  
        }
        string currentDirectory;
        WebBrowser browser;
        ImsManifest imsmanifest;
        public string CurrentDirectory
        {
            get { return this.currentDirectory; }
            set { this.currentDirectory = value; }
        }
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public WebBrowser WebBrowser
        {
            get { return this.browser; }
            set { this.browser = value; }
        }
        public RibbonBarRegion.RibbonBarHeader ParentChild
        {
            get { return this.parentChild; }
            set { this.parentChild = value; }
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            QuestionTypeDialog examDialog = new QuestionTypeDialog();
            examDialog.WebBrowser = browser;
            examDialog.Owner = this;
            examDialog.ParentChild = parentChild;
            examDialog.CurrentDirectory = currentDirectory;
            examDialog.Imsmanifest = imsmanifest;
            examDialog.ListQuestions = listQuestions;
            examDialog.IsAddNew = false;
            examDialog.ShowDialog();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            listQuestions.RemoveAt(listPossibleAnwser.SelectedIndex);
            List<QuestionTest> law = new List<QuestionTest>();
            for (int i = 0; i < listQuestions.Count; i++)
            {
                QuestionTest qi = listQuestions[i];
                qi.Id = (i + 1).ToString();
                law.Add(qi);
            }
            listQuestions.Clear();
            for (int i = 0; i < law.Count; i++)
            {
                listQuestions.Add(law[i]);
            }
        }
        private void ViewDetail_Click(object sender, RoutedEventArgs e)
        {
            if (listQuestions[listPossibleAnwser.SelectedIndex].QuestionType == "single")
            {
                SimpleChoiceQuestionDialog scq = new SimpleChoiceQuestionDialog();
                scq.ParentChild = parentChild;
                scq.Owner = parentChild;
                scq.CurrentDirectory = currentDirectory;
                scq.WebBrowser = browser;
                scq.Imsmanifest = imsmanifest;
                scq.IsAddNew = false;
                scq.QuestionTest = listQuestions[listPossibleAnwser.SelectedIndex];
                scq.ShowDialog();
            }
            else if (listQuestions[listPossibleAnwser.SelectedIndex].QuestionType == "true_false")
            {
                TrueFalseQuestionDialog tfq = new TrueFalseQuestionDialog();
                tfq.ParentCHild = parentChild;
                tfq.Owner = parentChild;
                tfq.CurrentDirectory = currentDirectory;
                tfq.WebBrowser = browser;
                tfq.Imsmanifest = imsmanifest;
                tfq.IsAddNew = false;
                tfq.QuestionTest = listQuestions[listPossibleAnwser.SelectedIndex];
                tfq.ShowDialog();
            }
            else if (listQuestions[listPossibleAnwser.SelectedIndex].QuestionType == "multiple")
            {
                MultipleChoiceQuestionDialog mcq = new MultipleChoiceQuestionDialog();
                mcq.ParentChild = parentChild;
                mcq.Owner = parentChild;
                mcq.CurrentDirectory = currentDirectory;
                mcq.WebBrowser = browser;
                mcq.Imsmanifest = imsmanifest;
                mcq.IsAddNew = false;
                mcq.QuestionTest = listQuestions[listPossibleAnwser.SelectedIndex];
                mcq.ShowDialog();
            }
            else if (listQuestions[listPossibleAnwser.SelectedIndex].QuestionType == "order")
            {
                OrderChoiceQuestionDialog ocq = new OrderChoiceQuestionDialog();
                ocq.ParentChild = parentChild;
                ocq.Owner = parentChild;
                ocq.CurrentDirectory = currentDirectory;
                ocq.WebBrowser = browser;
                ocq.Imsmanifest = imsmanifest;
                ocq.IsAddNew = false;
                ocq.QuestionTest = listQuestions[listPossibleAnwser.SelectedIndex];
                ocq.ShowDialog();
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            ManifestProcessing mp = new ManifestProcessing(imsmanifest);

            //need to check more contraint
            QuestionTestData qtd = new QuestionTestData();
            qtd.ListQuestions = listQuestions;
            qtd.ListObj = mp.getCompetencyObjecttives();
            HtmlDocument content = qtd.createCompetencyTest();
            browser.NavigateToString(content.HTML);
            Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnHelp_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
